package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.text.ParseException;
import java.util.Vector;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultRowSorter;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.SpringLayout;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.text.BadLocationException;
import javax.swing.text.MaskFormatter;

import CommonGui.NotEdditibleTable;
import DataObjects.ConstantObjects;
import DataObjects.RecipeRightList;
import DataObjects.TablesOfShift;
import DataObjects.TotalHoursInMonthAccess;
import Messages.MessageAddCommentAboutOverHrs;
import Messages.MessageAddOrUpdateWorkingHoursFromDoh;
import Messages.MessageAddWorkingHoursReply;
import Messages.MessageSavingReply;
import Messages.MessageShowAllWorkHours;
import Messages.MessageShowAllWorkHoursReply;

public class PfShowAllTableOfThisShift extends JPanel{
	
	private MainMenu mainMenu;
	private NotEdditibleTable dtm; 
	private JTable tbl;
	private Vector<TablesOfShift> tables;
	public int x;
	private int y;
	private Dimension screenSize;
	private Vector <String> tblheader;
	
	private JPanel searchTable;
	private JPanel insertWorkDayPanel=null;
	private JPanel buttPanel;
	
	private  JButton btnCancel;
	
	private JTextField btnNumOrder;
	private JTextField btnNumTable;
	
	private JScrollPane scrollPane;
	
	private Vector<String> selectedRowVec=null;
	
	public PfShowAllTableOfThisShift(MainMenu mm, Vector<TablesOfShift> tables)
	{
		super();
		screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		x=screenSize.width;
		y=screenSize.height;
		this.setLayout(null);
		this.mainMenu=mm;
		this.tables=tables;
	
		
		 
		initiliaze();
	}	
	
	private void initiliaze()
	{
	   	
		getscrollPane(5,5,1005,640);
		getButtonPanel(1005,32,60,610);
	
	}
	
	
	
	public JScrollPane getscrollPane(int x,int y,int width,int length)
	{
		scrollPane = new JScrollPane(getTblHours());
		scrollPane.setBounds(x,y,width,length);
		this.add(scrollPane);
		return scrollPane;
	}
	
	

	public JTable getTblHours()
	{
		tblheader=new Vector<String>();
		tblheader.add("מס' הזמנה");
		tblheader.add("מס' שולחן");
		tblheader.add("שעה");
		tblheader.add("כמות");
		tblheader.add("חיוב");
		tblheader.add("צורה");
		tblheader.add("סטטוס");
		  
		dtm=new NotEdditibleTable(parseVectorObjectToVectorVector(tables),tblheader);	 
		
		 
		tbl = new JTable(dtm);
		//tbl.setRowHeight(0,50);
		tbl.setRowSelectionInterval(0, 1);
		DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
		centerRenderer.setHorizontalAlignment( JLabel.CENTER );
		for(int x=0;x<7;x++){
			tbl.getColumnModel().getColumn(x).setCellRenderer( centerRenderer );
	        }
		
		tbl.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		tbl.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		tbl.setFont(new Font("David", Font.BOLD, 16));
		tbl.getTableHeader().setFont( new Font( "Arial" , Font.BOLD, 17 ));
		tbl.getTableHeader().setBackground(new Color(173,216,230));
		tbl.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 
	    tbl.setRowHeight(50);
		tbl.setFillsViewportHeight(true);
        tbl.setGridColor(new Color(25, 25, 112));
        tbl.setBackground(new Color(250, 235, 215));
        tbl.setForeground(new Color(0, 0, 0));
        tbl.setAutoCreateRowSorter(true);
                 
        tbl.addMouseListener(new MouseAdapter() {
			   public void mouseClicked(MouseEvent e) {
				   JTable target = (JTable)e.getSource();
			       int row = target.getSelectedRow();
			       if (e.getClickCount() == 1) {
			       /*if(row!=-1)
			       {			    	 
				       selectedRowVec=(Vector<String>) dtm.getDataVector().get(row);		     
				      
			       }*/
			     }
			     
			     if (e.getClickCount() == 2 && row!=-1){
			    	 addInsertWorkDayPanel();
			    	 
			         }
			     }
			   
			 });
                
        return tbl;
	}
	
	private JPanel getButtonPanel(int x,int y,int width,int length)
	{
	    buttPanel = new JPanel();
	 //   buttPanel.setBorder(new TitledBorder(null, "", TitledBorder.CENTER, TitledBorder.TOP, null, null));
	    buttPanel.setBackground(new Color(250, 235, 215));
		buttPanel.setForeground(new Color(0, 0, 0));
		buttPanel.setLayout(null);
		buttPanel.setBounds(x,y,width,length);
		buttPanel.add(getDownButton());
		buttPanel.add(getUpButton());
		this.add(buttPanel);
		
		return buttPanel;
	}
	
	private Vector<Vector> parseVectorObjectToVectorVector (Vector<TablesOfShift> vectorObj)
	{
		Vector<Vector> vectorvector=new Vector<Vector>();
		
		for(int i=0;i<vectorObj.size();i++)
		{
			
			Vector<String> vector=new Vector<String>();
			vector.add(Float.toString(vectorObj.get(i).getOrderId()));
			vector.add(Float.toString(vectorObj.get(i).getTableId()));
			vector.add((vectorObj.get(i).getDateOpen()));
			vector.add(vectorObj.get(i).getTime());
			vector.add(Integer.toString(vectorObj.get(i).getQuantityOfPeople()));
			vector.add(Integer.toString(vectorObj.get(i).getQuantityOfChildren()));
			vector.add(vectorObj.get(i).getStatus());
			vectorvector.add(vector);
			
		}
		
		return vectorvector;
		
		
		
	}
	
	public void addInsertWorkDayPanel()
	{
		
		if(insertWorkDayPanel!=null) this.remove(insertWorkDayPanel);
		if(tbl.getSelectedRowCount()==0) return;
		
		JPanel panel=getInsertWorkDayPanel(this);	
		this.remove(scrollPane);
		this.remove(buttPanel);
		this.validate();
		
		getscrollPane(5,5,750,640);
		getButtonPanel(750,32,55,610);
		this.add(panel);
		
		
		this.validate();
		this.repaint();
	}
	
	
	
	private JPanel getInsertWorkDayPanel(PfShowAllTableOfThisShift table)
	{
		
		insertWorkDayPanel = new JPanel();
		Border blackline=BorderFactory.createLineBorder(Color.black);
		
		insertWorkDayPanel.setBorder(blackline);
		insertWorkDayPanel.setBounds(805,32,255,610);
		insertWorkDayPanel.setBackground(new Color(255, 250, 205));
		insertWorkDayPanel.setLayout(null);
		
		insertWorkDayPanel.add(getBtnCancel(table));
		System.out.println("NUUU");
		insertWorkDayPanel.add(getBtnSaveCancel(table));
		System.out.println("NUUU");
		insertWorkDayPanel.add(getBtnNumberOrder(table));
		System.out.println("NUUU");
		insertWorkDayPanel.add(getBtnNumberTable(table));
		System.out.println("NUUU");
	
		return insertWorkDayPanel;
	}
	
	private JButton getDownButton()
	{
		Icon downIcon = new ImageIcon("down_icon.png");
		Image img = ((ImageIcon) downIcon).getImage();
		Image newimg = img.getScaledInstance(x/30, x/30,java.awt.Image.SCALE_SMOOTH);
		downIcon = new ImageIcon(newimg);
		JButton downButton=new JButton(downIcon);
		downButton.setVerticalTextPosition(AbstractButton.BOTTOM);
		downButton.setHorizontalTextPosition(AbstractButton.CENTER);
		downButton.setFont(new Font("Tahoma", Font.BOLD, x/90));
		downButton.setBounds(4, 10, 51, 66);
	//	downButton.setBounds(63*x/100, 11*y/23, 5*x/100, 2*y/24);
		downButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
				int index=tbl.getSelectedRow();
				if(index+1==tbl.getRowCount())
						index=-1;
				
				tbl.setRowSelectionInterval(index+1, index+1);
				
				if(insertWorkDayPanel!=null)
				{
					selectedRowVec=(Vector<String>) dtm.getDataVector().get(index+1);	
					btnNumOrder.setText(selectedRowVec.get(0));
					
				}
		    				
			}
		});
		
		
		
		
		return downButton;
	}
	
	
	private JButton getUpButton()
	{
		Icon upIcon = new ImageIcon("up_icon.png");
		Image img = ((ImageIcon) upIcon).getImage();
		Image newimg = img.getScaledInstance(x/30, x/30,java.awt.Image.SCALE_SMOOTH);
		upIcon = new ImageIcon(newimg);
		JButton upButton=new JButton(upIcon);
		upButton.setVerticalTextPosition(AbstractButton.BOTTOM);
		upButton.setHorizontalTextPosition(AbstractButton.CENTER);
		upButton.setFont(new Font("Tahoma", Font.BOLD, x/90));
		upButton.setBounds(4, 530, 51, 66);
	//	upButton.setBounds(63*x/100, 20*y/70, 5*x/100, 2*y/24);
		
		upButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
				int index=tbl.getSelectedRow();
				if(index==0 || index==-1)
					index=tbl.getRowCount();
				
				tbl.setRowSelectionInterval(index-1, index-1);
				if(insertWorkDayPanel!=null)
				{
					selectedRowVec=(Vector<String>) dtm.getDataVector().get(index-1);	
					btnNumOrder.setText(selectedRowVec.get(0));
					
				}
								
			}
		});
		
		return upButton;
	}
	
	
		
	
	
	/**
	 * Method for creation JButton btnCancel that close panel getInsertInfoPanel
	 * @return btnCancel
	 */
	private JButton getBtnCancel(final PfShowAllTableOfThisShift table)
	{
	    btnCancel=new JButton("לצאת");
		btnCancel.setBounds(5, 450, 125, 60);
		btnCancel.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnCancel.setBackground(Color.RED);
		btnCancel.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{	
				table.remove(insertWorkDayPanel);
			//	((JButton)e.getSource()).getParent().getParent().getParent().remove(((JButton)e.getSource()).getParent().getParent());
				table.add(getInsertWorkDayPanel(table));
				mainMenu.getContentPane().validate();
				mainMenu.getContentPane().repaint();
			//	mainMenu.repaint();
				
			}
			
			
		});
		return btnCancel;
	}
	
	private JButton getBtnSaveCancel(final PfShowAllTableOfThisShift table)
	{
	    btnCancel=new JButton("בטל");
		btnCancel.setBounds(125, 450, 125, 60);
		btnCancel.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnCancel.setBackground(Color.GREEN);
		btnCancel.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{	
			//	table.remove(insertWorkDayPanel);
				((JButton)e.getSource()).getParent().getParent().getParent().remove(((JButton)e.getSource()).getParent().getParent());
				
				mainMenu.getContentPane().validate();
				mainMenu.getContentPane().repaint();
			//	mainMenu.repaint();
				
			}
			
			
		});
		return btnCancel;
	}
	
	private JTextField getBtnNumberOrder(final PfShowAllTableOfThisShift table)
	{
        int index = tbl.getSelectedRow();
        if(index!=(-1))
        {
        	selectedRowVec=(Vector<String>) dtm.getDataVector().get(index);
        	btnNumOrder=new JTextField(selectedRowVec.get(0));
        }
			        
		else
			 btnNumOrder=new JTextField("aa");
	    btnNumOrder.setBounds(30, 150, 170, 50);
	    btnNumOrder.setFont(new Font("Tahoma", Font.BOLD, 14));
	   
	   
		return btnNumOrder;
	}
	
	
	private JTextField getBtnNumberTable(final PfShowAllTableOfThisShift table)
	{
		 int index = tbl.getSelectedRow();
		if(index!=(-1))
        {
        	selectedRowVec=(Vector<String>) dtm.getDataVector().get(index);
        	btnNumTable=new JTextField(selectedRowVec.get(1));
        }
		else
			btnNumTable=new JTextField("");
	    btnNumTable.setBounds(30, 250, 170, 50);
	    btnNumTable.setFont(new Font("Tahoma", Font.BOLD, 14));
	   
	   
		return btnNumTable;
	}
	
	
	
	
}
